<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Org\Image;

class ConfigController extends Controller
{
    /**
    *   网站配置  添加配置
    */
    public function getAdd()
    {
        return view("admin/config/add");
    }

    /**
    *   添加网站配置
    */
    public function postInsert(Request $request)
    {
        $arr = $request->except("_token",'logo');

         if($request->hasFile('logo')) {

            //随机号
            $name = rand(10000,99999).time();
            //后缀名
            $suffix = $request->file('logo')->getClientOriginalExtension();
           
            //图片文件
            $filename = $name.'.'.$suffix;   //执行图片压缩文件

            //存入本地
            $request->file('logo')->move('./upload/',$filename);

            //new对象
            $img = new Image;
            //调用方法
            $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

           //写入数据库
           $arr['logo'] = "/upload/s_".$filename;

           //删除大图
           @unlink("./upload/".$filename);

        }

        $res = DB::table('project_webconf')->insert($arr);

        if($res) {
            return redirect("/Admin/config/index")->with("success","添加成功");
        } else {
            return back()->with("error","添加失败");
        }
    }

    /**
    *   显示浏览配置文件
    */
    public function getIndex()
    {
       $res = DB::table('project_webconf')->get();

       return view("admin/config/index",['res'=>$res]);
    }

    /**
    *   修改网站配置文件页面
    */
    public function getEdit(Request $request)
    {
        $id = $request->input("id");

        $res = DB::table("project_webconf")->where("id",$id)->first();

        return view("admin/config/edit",["res"=>$res]);
    }

    /**
    *   修改方法
    */
    public function postUpdate(Request $request)
    {
         $arr = $request->except("_token",'logo','id');
         $id = $request->only('id');

         if($request->hasFile('logo')) {

            //随机号
            $name = rand(10000,99999).time();
            //后缀名
            $suffix = $request->file('logo')->getClientOriginalExtension();
           
            //图片文件
            $filename = $name.'.'.$suffix;   //执行图片压缩文件

            //存入本地
            $request->file('logo')->move('./upload/',$filename);

            //new对象
            $img = new Image;
            //调用方法
            $img->open('./upload/'.$filename)->thumb(100,100)->save('./upload/s_'.$filename);

           //写入数据库
           $arr['logo'] = "/upload/s_".$filename;

           //获取就图片
          $row = DB::table("project_webconf")->where("id",$id)->first()->logo;

           //删除旧图  删除大图
          @unlink(".".$row);
          @unlink("./uplaod".$filename);
        }


        $res = DB::table('project_webconf')->where('id',$id)->update($arr);

        if($res) {
            return redirect("/Admin/config/index")->with("success","修改成功");
        } else {
            return back()->with("error","未做任何修改");
        }
    }

    /**
    *   删除网站配置
    */
    public function getDel(Request $request)
    {
        $id = $request->input('id');

        $res = DB::table("project_webconf")->where("id",$id)->delete();

        if($res) {
            return redirect("/Admin/config/index")->with("success","删除成功");
        } else {
             return redirect("/Admin/config/index")->with("error","删除失败");
        }
    }

}
